<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false" style="padding-bottom: 30px">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="归属公司" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="companyId">
              <a-input disabled v-model="model.companyId_dictText" placeholder="请输入公司名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审计年份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditYear">
              <a-select :getPopupContainer="(node) => node.parentNode" placeholder="请选择审计年份" :disabled="formDisabled" v-model="model.auditYear">
                <a-select-option :value="undefined">请选择</a-select-option>
                <a-select-option v-for="item in yearOptions" :key="item.value" :value="item.value">
                  <span style="display: inline-block;width: 100%" :title="item.title">
                    {{ item.title  }}
                  </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-collapse v-model="activeKey">
          <a-collapse-panel key="1" header="基础信息">
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item label="注册资金(万元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="registeredCapital">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.registeredCapital"
                    placeholder="请输入注册资本"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="净资产(万元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="netAssets">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.netAssets"
                    placeholder="请输入净资产"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="总资产(万元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalAssets">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.totalAssets"
                    placeholder="请输入总资产"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="固定资产(原值/净值)(万元)"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="fixedAssets"
                >
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.fixedAssets"
                    placeholder="请输入固定资产(原值/净值)"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="流动资产(万元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currentAssets">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.currentAssets"
                    placeholder="请输入流动资产"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="流动负债(万元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currentLiabilities">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.currentLiabilities"
                    placeholder="请输入流动负债"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="负债合计(万元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="totalLiabilities">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.totalLiabilities"
                    placeholder="请输入负债合计"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="营业收入(万元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="operatingIncome">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.operatingIncome"
                    placeholder="请输入营业收入"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="现金流量净额(万元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cashFlow">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.cashFlow"
                    placeholder="请输入现金流量净额"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
          <a-collapse-panel key="2" header="主要财务指标">
            <a-row :gutter="48">
              <a-col :span="8">
                <a-form-model-item
                  label="净资产收益率(%)"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="incomeRate"
                >
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.incomeRate"
                    placeholder="请输入净资产收益率"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="总资产报酬率(%)"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="compensationRate"
                >
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.compensationRate"
                    placeholder="请输入总资产报酬率"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item
                  label="主营业务利润率(%)"
                  :labelCol="labelCol"
                  :wrapperCol="wrapperCol"
                  prop="profitRate"
                >
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.profitRate"
                    placeholder="请输入主营业务利润率"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="资产负债率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="liabilityRate">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.liabilityRate"
                    placeholder="请输入资产负债率"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="流动比率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currentRatio">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.currentRatio"
                    placeholder="请输入流动比率"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="速动比率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="quickRatio">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.quickRatio"
                    placeholder="请输入速动比率"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="8">
                <a-form-model-item label="利润率(%)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="profitMargin">
                  <a-input-number
                    :disabled="formDisabled"
                    :max="99999999"
                    :precision="2"
                    v-model="model.profitMargin"
                    placeholder="请输入利润率"
                    style="width: 100%"
                  />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachments">
                  <j-upload v-if="!disabled" :disabled="formDisabled" v-model="model.attachments" filePdf isSingle></j-upload>
                  <view-upload v-else disabled v-model="model.attachments"></view-upload>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-collapse-panel>
        </a-collapse>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { mapGetters } from 'vuex'
import moment from 'moment'
import ViewUpload from '@/views/components/ViewUpload.vue'

export default {
  name: 'ErpFinancialReportForm',
  components: { ViewUpload },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        companyId: [{ required: true, message: '请输入公司名称!', trigger: 'blur' }],
        auditYear: [{ required: true, message: '请选择审计年份!', trigger: ['change', 'blur'] }],
        attachments: [{ required: true, message: '请选择附件!', trigger: ['change', 'blur'] }],
        registeredCapital: [{ required: true, message: '请输入注册资本!', trigger: 'blur' }],
        netAssets: [{ required: true, message: '请输入净资产!', trigger: 'blur' }],
        totalAssets: [{ required: true, message: '请输入总资产!', trigger: 'blur' }],
        fixedAssets: [{ required: true, message: '请输入固定资产(原值/净值)!', trigger: 'blur' }],
        currentAssets: [{ required: true, message: '请输入流动资产!', trigger: 'blur' }],
        currentLiabilities: [{ required: true, message: '请输入流动负债!', trigger: 'blur' }],
        totalLiabilities: [{ required: true, message: '请输入负债合计!', trigger: 'blur' }],
        cashFlow: [{ required: true, message: '请输入现金流里净额!', trigger: 'blur' }],
        operatingIncome: [{ required: true, message: '请输入营业收入!', trigger: 'blur' }],
        incomeRate: [{ required: true, message: '请输入净资产收益率!', trigger: 'blur' }],
        compensationRate: [{ required: true, message: '请输入总资产报酬率!', trigger: 'blur' }],
        profitRate: [{ required: true, message: '请输入主营业务利润率!', trigger: 'blur' }],
        liabilityRate: [{ required: true, message: '请输入资产负债率!', trigger: 'blur' }],
        currentRatio: [{ required: true, message: '请输入流动比率!', trigger: 'blur' }],
        quickRatio: [{ required: true, message: '请输入速动比率!', trigger: 'blur' }],
        profitMargin: [{ required: true, message: '请输入利润率!', trigger: 'blur' }],
      },
      url: {
        add: '/api/erp/files/company/erpCompanyAudit/add',
        edit: '/api/erp/files/company/erpCompanyAudit/edit',
        queryById: '/api/erp/files/company/erpCompanyAudit/queryById',
      },
      activeKey: ['1', '2'],
      yearOptions: []
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
    this.getYearSource()
  },
  methods: {
    ...mapGetters(['userInfo']),
    getYearSource(){
      let data = []
      for (let index = 0; index <= 10; index++) {
        if(index==0){
          data.push({ title: `${new Date().getFullYear()}`,value: `${new Date().getFullYear()}` })
        } else {
          data.push({ title: `${new Date().getFullYear()-index}`,value: `${new Date().getFullYear()-index}` })
          data.push({ title: `${index+new Date().getFullYear()}`,value: `${index+new Date().getFullYear()}` })
        }
      }
      this.yearOptions = data.sort((a,b) => a.value-b.value)
    },
    add(model) {
      this.modelDefault.companyId = model.companyId
      this.modelDefault.companyId_dictText = model.companyId_dictText
      this.edit(this.modelDefault,model)
    },
    edit(record,data) {
      this.$refs['form'].clearValidate()
      this.model = Object.assign({}, record)
      this.model.companyId_dictText = data.companyId_dictText
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
                that.$emit('unButtonDisable')
              }
            })
            .catch((error) => {
              that.$emit('unButtonDisable')
            })
            .finally(() => {
              that.confirmLoading = false
            })
        } else {
          that.$emit('unButtonDisable')
        }
      })
    },
  },
}
</script>
